const http = require("axios");
const moment = require("moment");

module.exports = {
  pushMessageToUser: (req) => {
    const { formid, skey, content } = req.body;
    let access_token = "",
      openid = "",
      uname = "",
      utime = moment().format("YYYY-MM-DD HH:mm:ss");

    // 初始化模板id
    const template_id = "",
      page = "/pages/books/books";

      // 获取当前小程序调用接口的凭证
    push
      .getPusherToken()
      .then((res) => {
        if (res && res[0] && res[0].token) {
          access_token = res[0].token;
        }
        return User.getUserId(skey, content);
      })
      .then((res) => {
        if (res && res[0] && res[0].uid) {
          openid = res[0].openid;
          uname = res[0].uname;
          utime = res[0].ctime;
        }
        if (!access_token && !openid) {
          throw new Error("信息过去失败");
        } else {
          const params = {
            touser: openid,
            template_id,
            page,
            form_id: formid,
            data: {
              keyword1: {
                value: "ddfaw",
                color: "#fff000",
              },
              keyword2: {
                value: uname+"您的留言入选了",
                color: "#fff000",
              },
              keyword3: {
                value: utime,
                color: "#fff000",
              },
            },
          };
          return http({
            url: "", // 微信模板消息接口
            method: "POST",
            data: params
          })
        }
      }).then(res => {

      }).catch(err => {
        
      })
  },
};
